#!/bin/bash
ip_mediator_prod1=10.47.61.114
ip_mediator_prod2=10.27.62.198
ip_mediator_stage=10.168.3.124
#ip_mytijian_stage=10.117.26.117
ip_mediator1=10.47.61.114

IP_VAR=ip_$server

remote_host=jenkins@${!IP_VAR} #原本为root
mvn="/opt/jenkins_slave/tools/hudson.tasks.Maven_MavenInstallation/maven3.3.9/bin/mvn"

#WORKSPACE="/opt/jenkins_slave/WORKSPACE/Prod_App_Build_NoDocker"
#WORKSPACE="/opt/jenkins_slave/WORKSPACE/Prod_Tomcat_App_Deploy"
:>isGitCloneSuccess.log #记录拉取代码是否成功
:>appconfigure.log #记录拉取配置文件

function build(){
    #是否需要编译
    echo "==========================代码编译开始==============================="
    
    #清空目录
    if [ -d ${WORKSPACE}/${project} ];then
        rm -rf ${WORKSPACE}/${project} 
    fi
    
    cd ${WORKSPACE}
    #echo $WORKSPACE -> /opt/jenkins_slave/WORKSPACE/Prod_App_Build_NoDocker_Test
    #拉取代码
    git clone git@gitee.com:mytj/${project}.git >> isGitCloneSuccess.log 2>&1 
    #拉取配置文件
    #git clone -b master git@gitee.com:mytj/${config_name}.git >> appconfigure.log 2>&1
    
    #编译
    cd ${WORKSPACE}/${project}
    ${mvn} clean install -Pver -U
    if [ $? -eq  0 ];then
        echo "==========================代码编译成功==============================="
    fi
}

function  deploy(){
    if [ "${project}" == "mediator-ws" ];then
        echo "=============================mediator-ws代码发布开始================================"
        TOMCAT_DIR="/opt/tomcat"
        MED_DIR="/mytijian/mediator"
        
        cd ${WORKSPACE} #/opt/jenkins_slave/WORKSPACE/Prod_Tomcat_App_Deploy
        
        if [ "X$server" = "Xmediator_stage" ];then
            version="mediator-ws-4.0"
            CONFIG=${WORKSPACE}/environments/stage/FileList
        else
            version="mediator-ws-3.0"
            CONFIG=${WORKSPACE}/environments/prod/FileList
        fi
        
        ssh ${remote_host} "${MED_DIR}/bin/tomcat stop"
        if [ $? -eq 0 ];then
            echo "tomcat 关闭成功"
        else
            echo "tomcat 关闭失败" && exit 1
        fi
        
        war_name=`ls ${WORKSPACE}/mediator-ws/mediator-ws/target | grep war` #/opt/jenkins_slave/WORKSPACE/Prod_App_Build_NoDocker
        #echo $war_name
        
        ssh ${remote_host} "ls ${MED_DIR}/$version"
        if [ $? = 0 ];then
            ssh ${remote_host} "rm -rf ${MED_DIR}/$version/*"
        else
            ssh ${remote_host} "mkdir ${MED_DIR}/$version"
        fi
        
        scp ${WORKSPACE}/mediator-ws/mediator-ws/target/${war_name} ${remote_host}:${MED_DIR}/${version}/
        
        #手动解压
        ssh ${remote_host} "unzip -o ${MED_DIR}/${version}/${war_name} -d ${MED_DIR}/${version} && rm -f ${MED_DIR}/${version}/${war_name}"
        
        cd ${CONFIG} 
        [ $? != 0 ]&& exit 0
        
        scp mediator-mongodb.properties ${remote_host}:${MED_DIR}/$version/WEB-INF/classes/mongodb.properties
        scp mediator-config.properties ${remote_host}:${MED_DIR}/$version/WEB-INF/classes/config.properties
        scp mediator-log4j.xml ${remote_host}:${MED_DIR}/$version/WEB-INF/classes/log4j.xml
        if [ $? -eq 0 ];then
            echo "成功替换配置文件"
        else
            echo "替换配置文件失败" && exit 1
        fi
        
        #开启tomcat
        ssh ${remote_host} "${MED_DIR}/bin/tomcat start"
        if [ $? -eq 0 ];then
            echo "tomcat 开启成功"
        else
            echo "tomcat 开启失败" && exit 1
        fi
        
        sleep 30
        
        isSuccess=`curl -I -m 10 -o /dev/null -s -w %{http_code} "https://spi.mytijian.com/${version}/services"`
        #echo $isSuccess
        if [ "$isSuccess" == "200" ] || [ "$isSuccess" == "302" ];then
            echo "===============代码发布成功==============="
        else
            echo "===============代码发布失败===============" && exit 1
        fi
    elif [ "${project}" == "mediator-client" ];then
        echo "=============================mediator-client代码发布开始================================"
        #读取版本
        version_entire=`cat ${WORKSPACE}/${project}/pom.xml | grep "<version>" | sed -n '1p'` #/opt/jenkins_slave/WORKSPACE/Prod_Tomcat_App_Deploy
        #截取版本号
        version_left=${version_entire##*<version>}
        version=${version_left%%</version>*}
        #echo ${version}
    
        #判断文件夹是否存在
        if ssh ${remote_host} [ ! -d /mytijian/agent/client/${version}/probes ];then 
            ssh ${remote_host} "mkdir -p /mytijian/agent/client/${version}/probes"   
        fi  
        
        #代码发布
        #判断mediator1是否存在probes目录,将probes下所有jar包发送到mediator/probes
        #将apps下zip或war包发送到client/$version
        echo "============================发送probes包============================="
    
        for probes_file in `find ${WORKSPACE}/${project}/probes -type f | grep -E "\.zip$"`
        do
            scp ${probes_file} ${remote_host}:/mytijian/agent/client/${version}/probes
        done
        if [ $? -eq 0 ];then
            echo "============================发送probes包成功============================="
        fi
    
        echo "============================发送control和mediator-agent包============================="
        for control_file in `find ${WORKSPACE}/${project}/apps -type f | grep -E "\.war$|\.zip$"`
        do
            scp ${control_file} ${remote_host}:/mytijian/agent/client/${version}/
        done
        if [ $? -eq 0 ];then
            echo "============================发送control和mediator-agent包成功============================="
        fi
        echo "=============================mediator-client代码发布结束================================"
    else
        echo "=============================mediator-webservice代码发布开始================================"
        #读取版本
        version_entire=`cat ${WORKSPACE}/${project}/pom.xml |grep "<version>" |sed -n '1p'` #/opt/jenkins_slave/WORKSPACE/Prod_Tomcat_App_Deploy
        #截取版本号
        version_left=${version_entire##*<version>}
        version=${version_left%%</version>*}
        #echo ${version}
        
        ssh ${remote_host} "ls /mytijian/agent/client/webservice/${version}"
        if [ $? != 0 ];then
            ssh ${remote_host} "mkdir /mytijian/agent/client/webservice/${version}" >>/dev/null
        else
            ssh ${remote_host} "rm -rf /mytijian/agent/client/webservice/${version}/*" >>/dev/null
        fi
         
        echo "======copy control and mediator-agent war======"
        for file in `find $WORKSPACE/${project} -type f |grep -E "\.war$"`
        do      
            scp $file ${remote_host}:/mytijian/agent/client/webservice/$version
        done 
                
        echo "=============================mediator-webservice代码发布结束================================"
    fi
}

if [ "$isBuild" == "true" ];then
    build
fi

if [ "$isDeploy" == "true" ];then
    deploy
fi